<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Default_TraCuuController
 *
 * @author NghiaPHT
 */
class default_TracuuController extends ZF_DefaultController{
    public function indexAction() {        
        $this->view->header = "Tra cứu thông tin khách hàng";        
        
        $model_daily = new Default_Model_Daily();        
        $model_loaidaily = new Default_Model_Loaidaily();
        $model_trackdonhang = new Default_Model_Trackdonhang();
        $model_donhang = new Default_Model_Donhang();
        $model_chitietdonhang = new Default_Model_Chitietdonhang();
        
        
        
        if($this->getRequest()->isPost()){
            $madaily = $this->getRequest()->getParam("madaily");
            $cmnd = $this->getRequest()->getParam("cmnd");
            $thang = $this->getRequest()->getParam("thang");
            $nam = $this->getRequest()->getParam("nam");
            $this->view->madaily = $madaily;
            $this->view->cmnd = $cmnd;
            $this->view->thang = $thang;
            $this->view->nam = $nam;
            
            $daily = $model_daily->getById($madaily);
            if(!$madaily || !$cmnd || !$thang || !$nam){
                $this->view->error = "Bạn chưa nhập đủ thông tin!";                
            }
            else if(!$daily){
                $this->view->error = "Mã đại lý không tồn tại!";                
            }
            else if($daily['SoCMND'] != $cmnd){
                $this->view->error = "Bạn nhập sai số CMND, vui lòng nhập lại";
            }
            else{                
                $matrackdonhang = $madaily . "_" . $thang . "_" . $nam;
                $trackdonhang = $model_trackdonhang->getById($matrackdonhang);
                $loinhuan = 0;
                $bonus = 0;
                if($trackdonhang) {                    
                    $loinhuan = $trackdonhang['LoiNhuan'];
                    $bonus = $trackdonhang['Bonus'];
                }             
                $this->view->daily = $daily;
                $this->view->loinhuan = $loinhuan;
                $this->view->bonus = $bonus;
                
                // render dai ly cha
                $dailycha = $model_daily->getById($daily['MaDaiLyCha']);
                if($dailycha && $dailycha != -1){
                    $this->view->dailycha = $dailycha['TenDaiLy'];
                }
                else{
                    $this->view->dailycha = "StemPower";
                }
                
                // render loai dai ly
                $loaidaily = $model_loaidaily->getById($daily['MaLoaiDaiLy']);
                if($loaidaily && $loaidaily != -1){
                    $this->view->loaidaily = $loaidaily['TenLoaiDaiLy'];
                }
                else{
                    $this->view->loaidaily = "Đại lý bình thường";
                }
                
                // render don hang tu ban
                $tableDataDHTB = array();
                $donhangtutbanStr = $trackdonhang['DSMaDHTuBan'];
                if($donhangtutbanStr && $donhangtutbanStr != null && $donhangtutbanStr != ""){
                    $danhsachdonhangtutban = explode(",", $donhangtutbanStr);
                    if($danhsachdonhangtutban){
                        foreach ($danhsachdonhangtutban as $madonhangtuban){
                            $donhangtuban = $model_donhang->getById($madonhangtuban);
                            if($donhangtuban && $donhangtuban != -1){
                                $chitietdonhangStr = $donhangtuban['DSMaCTDH'];
                                if($chitietdonhangStr && $chitietdonhangStr != null && $chitietdonhangStr != ""){
                                    $danhsachCTDH = explode(",", $chitietdonhangStr);
                                    if($danhsachCTDH){
                                        foreach ($danhsachCTDH as $maCTDH){
                                            $chitietdonhang = $model_chitietdonhang->getById($maCTDH);
                                            if($chitietdonhang && $chitietdonhang != -1){
                                                $tendailyban = $this->getNameDaiLy($donhangtuban['MaDaiLyBan']);
                                                $tendailymua = $this->getNameDaiLy($donhangtuban['MaDaiLyMua']);
                                                $tensanpham = $this->getNameSanPham($chitietdonhang['MaSanPham']);
                                                $dongia = $this->getDonGiaSanPham($chitietdonhang['MaSanPham']);
                                                $hoahong = $this->getHoaHong($donhangtuban['MaDaiLyBan']);
                                                
                                                //tạo row data.
                                                $rowtable = array(  
                                                    'nameDLBan' => $tendailyban,
                                                    'nameDLMua'=> $tendailymua,
                                                    'nameSP' => $tensanpham,
                                                    'donGia'=> $dongia,
                                                    'soLuong'=>$chitietdonhang['SoLuong'],
                                                    'thue'=>$chitietdonhang['Thue'],
                                                    'ngayBan'=>$donhangtuban['NgayMuaHang'],
                                                    'thanhTien'=>$chitietdonhang['ThanhTien'],
                                                    'loiNhuan'=> $hoahong * 0.01 * $chitietdonhang['ThanhTien']
                                                 );
                                                // add table $tableDataDHTB
                                                $tableDataDHTB[] = $rowtable;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                $this->view->tableDataDHTB = $tableDataDHTB;
                
            }            
        }
        else{
            $this->view->thang = date("m");
            $this->view->nam = date("Y");            
        }
    }
    
    public function getNameDaiLy($madaily){
        $ret = "";
        
        $model_daily = new Default_Model_Daily();
        $daily = $model_daily->getById($madaily);
        if($daily && $daily != -1){
            $ret = $daily['TenDaiLy'];
        }       
        
        return $ret;
    }
    
    public function getNameSanPham($masanpham){
        $ret = "";
        
        $model_sanpham = new Default_Model_Sanpham();
        $sanpham = $model_sanpham->getById($masanpham);
        if($sanpham && $sanpham != -1){
            $ret = $sanpham['TenSanPham'];
        }       
        
        return $ret;
    }
    
    public function getDonGiaSanPham($masanpham){
        $ret = 0;
        
        $model_sanpham = new Default_Model_Sanpham();
        $sanpham = $model_sanpham->getById($masanpham);
        if($sanpham && $sanpham != -1){
            $ret = $sanpham['DonGia'];
        }       
        
        return $ret;
    }
    
    public function getHoaHong($madaily){
        $ret = 0;
        
        $model_daily = new Default_Model_Daily();
        $daily = $model_daily->getById($madaily);
        if($daily && $daily != -1){
            $model_loaidaily = new Default_Model_Loaidaily();
            $loaidaily = $model_loaidaily->getById($daily['MaLoaiDaiLy']);
            if($loaidaily && $loaidaily != -1){
                $ret = $loaidaily['PhanTramHoaHong'];
            }
        }       
        
        return $ret;
    }
}

?>